<?php
/*
 * Session 类
 * @author     Mark <zhaody901@126.com>
 * @copyright  Copyright (c) 2013 lemon team
 * @version    $Id: 0f7be2cc721749cb962b5e2f068b1b31b0e390ac $
 * @time       14-2-7 上午10:29
 */
defined('IN_IXDCW') or exit('Access Denied');

/**
 * session类
 */
class Session {
	static $handler;
	/**
	 * 初始化session
	 */
	public static function start() {
		global $CFG;

		if (!self::$handler) {
			if($CFG['cookie_domain']) @ini_set('session.cookie_domain', $CFG['cookie_domain']);
			// 最长的垃圾回收时间
			@ini_set('session.gc_maxlifetime', 1800);
			session_cache_limiter('private, must-revalidate');
			require_once DT_INC.'/PSessionHandler.class.php';
			self::$handler = new PSessionHandler();
			session_set_save_handler(
				array(self::$handler, 'open'),
				array(self::$handler, 'close'),
				array(self::$handler, 'read'),
				array(self::$handler, 'write'),
				array(self::$handler, 'destroy'),
				array(self::$handler, 'gc')
			);
			@session_start();
			header("cache-control: private");
		}
	}

	/**
	 * 检测是否存在某变量
	 * @param $seName
	 * @return bool
	 */
	public static function has($seName) {
		Session::start();
		if (isset($_SESSION[$seName])) {
			return true;
		}
		return false;
	}

	/**
	 * 移除session
	 * @param $seName
	 * @return bool
	 */
	public static function del($seName) {
		Session::start();
		if (!isset($_SESSION[$seName])) {
			return true;
		} else {
			unset($_SESSION[$seName]);
		}
	}

	/**
	 * 获取Session
	 * @param $seName
	 * @return null
	 */
	public static function get($seName) {
		if (!isset($_SESSION[$seName])) {
			return null;
		} else {
			return $_SESSION[$seName];
		}
	}

	/**
	 * 设置Session的值
	 * @param $key
	 * @param $value
	 */
	public static function set($key, $value){
		$_SESSION[$key] = $value;
	}


	/**
	 * 获取键, 用于删除图片
	 * @param $value
	 * @return int|string
	 */
	public static function key($value) {
		Session::start();
		foreach($_SESSION as $k => $v) {
			if ($v == $value) {
				return $k;
			}
		}
		return '';
	}
}